<!--
 * @Author: 千锋爱佛僧
 * @公众号: 大前端私房菜
 * @Slogan: 千锋精品教程，好学得不像实力派！
-->
<!--
 * @Author: 千锋爱佛僧
 * @公众号: 大前端私房菜
 * @Slogan: 千锋精品教程，好学得不像实力派！
-->
<template>
  <el-form
    :model="ruleForm"
    :rules="rules"
    ref="ruleForm"
    label-width="100px"
    class="demo-ruleForm"
  >
    <el-form-item label="分类名称" prop="name">
      <el-input v-model="ruleForm.name"></el-input>
    </el-form-item>
    <el-form-item label="父级类目" prop="parent">
      <el-select v-model="ruleForm.parent" placeholder="请选择归属分类">
        <el-option label="顶级类目" value="0-0"></el-option>
        <el-option
          v-for="item in cfyList"
          :label="item.name"
          :value="item.objectId"
        ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="是否上架" prop="online">
      <el-switch v-model="ruleForm.online"></el-switch>
    </el-form-item>
    <el-form-item label="分类图标" prop="icon">
      <img-upload v-model:changeImg="ruleForm.icon" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')"
        >立即更新</el-button
      >
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { classifyPut, classifyGet } from "@/api/job";
export default {
  data() {
    return {
      ruleForm: {
        name: "",
        parent: "",
        online: "",
        icon: "",
      },
      rules: {
        name: [{ required: true, message: "请输入分类名称", trigger: "blur" }],
        parent: [
          { required: true, message: "请选择父级类目", trigger: "blur" },
        ],
        online: [
          { required: true, message: "请控制分类状态", trigger: "blur" },
        ],
        icon: [{ required: true, message: "请上传分类图标", trigger: "blur" }],
      },
      cfyList: [], //分类列表
    };
  },
  created() {
    classifyGet().then((res) => {
      console.log(res);
      this.cfyList = res.data.results;
    });
    //获取列表页传递的路由query参数
    let { query } = this.$route;
    // console.log("待编辑数据", eval(query.online1));
    query.online = eval(query.online1);
    let { name, icon, online, parent } = query;
    this.ruleForm = { name, icon, online, parent };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //如何获取表单数据
          console.log(this.ruleForm);
          let { objectId } = this.$route.query; //提取被更新的分类id
          classifyPut(objectId, this.ruleForm);
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
